package org.aiforum.backend.common;

import java.util.List;

import org.aiforum.domain.Admin;
import org.aiforum.domain.Auth;

/**
 * 管理员帮助类
 * @author JAVA
 *
 */
public class AdminHelper {
	
	/**
	 * 判断是否登录
	 * @return
	 */
	public static boolean hasLogin(String token){
		return null != CacheContext.getCache(token, Admin.class);
	}
	
	/**
	 * 获取当前登录管理员账号
	 * @return
	 */
	public static String getCurrentAccount(String token){
		Admin admin = CacheContext.getCache(token, Admin.class);
		if(null != admin){
			return admin.getAccount();
		}
		return null;
	}
	
	/**
	 * 获取当前登录管理员ID
	 * @return
	 */
	public static int getCurrentAdminId(String token){
		Admin admin = CacheContext.getCache(token, Admin.class);
		if(null != admin){
			return admin.getAdminId();
		}
		return 0;
	}
	
	/**
	 * 获取当前登录管理员头像
	 * @return
	 */
	public static String getCurrentAdminHead(String token){
		Admin admin = CacheContext.getCache(token, Admin.class);
		if(null != admin){
			return admin.getHead();
		}
		return null;
	}

	/**
	 * 检查管理员是否有权限执行操作
	 * @param token
	 * @param url
	 * @return
	 */
	public static boolean hasAuthOperate(String token, String url) {
		boolean state = false;
		Admin admin = CacheContext.getCache(token, Admin.class);
		if(null != admin){
			if(null != admin.getAuths() && admin.getAuths().size() > 0){
				List<Auth> auths = admin.getAuths();
				for(Auth auth : auths){
					if(url.indexOf(auth.getAuthPath()) >= 0){
						state = true;
						break;
					}
				}
			}
		}
		return state;
	}
}
